﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Modelo_ETdA.Negocio.Analise;
using Modelo_ETdA.Negocio.Analise.Formato;

namespace Modelo_ETdA.Interface
{
    public partial class FormatoClienteCont : Form
    {

        private MenuAnaliseETdA menuAnaliseetda;
        private RegistaFormatoCliente regfCli;
        private FormatoClienteCont anterior;
        private FormatoClienteCont next;
        private List<Consideracao> lConsideracoes;
        private GestorAnalise gestor_analise;
        private int indiceConsi;
        DataTable cons1 = new DataTable();
        DataTable cons2 = new DataTable();
        DataTable cons3 = new DataTable();
        DataTable cons4 = new DataTable();

        public FormatoClienteCont(MenuAnaliseETdA menu, RegistaFormatoCliente regfCli, FormatoClienteCont ant, List<Consideracao> lcons, int indice, GestorAnalise gestana)
        {
            InitializeComponent();
            gestor_analise = gestana;
            menuAnaliseetda = menu;
            this.regfCli = regfCli;
            anterior = ant; // pode ser nulo!!!
            next = null;
            lConsideracoes = lcons;
            indiceConsi = indice;
            nomeCons1.Text = lConsideracoes[indiceConsi].Descricao;
            regfCli.inicializaTabelas(consideracao1, cons1);
            if (lConsideracoes.Count > indiceConsi + 1)
            {
                nomeCons2.Text = lConsideracoes[indiceConsi + 1].Descricao;
                regfCli.inicializaTabelas(consideracao2, cons2);

                if (lConsideracoes.Count > indiceConsi + 2)
                {
                    nomecons3.Text = lConsideracoes[indiceConsi + 2].Descricao;
                    regfCli.inicializaTabelas(consideracao3, cons3);

                    if (lConsideracoes.Count > indiceConsi + 3)
                    {
                        nomecons4.Text = lConsideracoes[indiceConsi + 3].Descricao;
                        regfCli.inicializaTabelas(consideracao4, cons4);
                    }
                    else
                    {
                        invisivel4();
                    }
                }
                else
                {
                    invisivel3();
                }
            }
            else
            {
                invisivel2();
            }

        }

        public void invisivel2()
        {
            consideracao2.Visible = false;
            nomeCons2.Visible = false;
            labcons2.Visible = false;
            invisivel3();
        }

        public void invisivel3()
        {
            consideracao3.Visible = false;
            nomecons3.Visible = false;
            labcons3.Visible = false;
            invisivel4();
        }

        public void invisivel4()
        {
            consideracao4.Visible = false;
            nomecons4.Visible = false;
            labcons4.Visible = false;
            regist.Visible = true;
            novaCon.Visible = false;
        }

        private void voltar_Click(object sender, EventArgs e)
        {
            if (anterior == null)
            {
                this.Visible = false;
                regfCli.Visible = true;
            }
            else
            {
                this.Visible = false;
                anterior.Visible = true;
            }

        }

        private void consideracao1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            regfCli.testaInput(consideracao1, e);
        }

        private void consideracao2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            regfCli.testaInput(consideracao2, e);
        }

        private void consideracao3_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            regfCli.testaInput(consideracao3, e);
        }

        private void consideracao4_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            regfCli.testaInput(consideracao4, e);
        }

        private void FormatoClienteCont_FormClosed(object sender, FormClosedEventArgs e)
        {
            menuAnaliseetda.sair();
        }

        private void novaCon_Click(object sender, EventArgs e)
        {
            try 
            {
            if (validaDados() == 1) return;

            if (next != null)
            {
                next.Visible = true;
                this.Visible = false;
            }
            else
            {
                FormatoClienteCont formato = new FormatoClienteCont(menuAnaliseetda, regfCli, this, lConsideracoes, indiceConsi + 4, gestor_analise);
                next = formato;
                formato.Visible = true;
                this.Visible = false;
            }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Não foi possível satisfazer o seu pedido!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public int validaDados()
        {
            try
            {

                if (regfCli.verificaDadosConsideracao(cons1, lConsideracoes[indiceConsi].Descricao) == 1) return 1;

                if (this.consideracao2.Visible == true)
                {
                    if (regfCli.verificaDadosConsideracao(cons2, lConsideracoes[indiceConsi + 1].Descricao) == 1) return 1;

                    if (this.consideracao3.Visible == true)
                    {
                        if (regfCli.verificaDadosConsideracao(cons3, lConsideracoes[indiceConsi + 2].Descricao) == 1) return 1;

                        if (this.consideracao4.Visible == true)
                            if (regfCli.verificaDadosConsideracao(cons4, lConsideracoes[indiceConsi + 3].Descricao) == 1) return 1;
                    }
                }
            }
            catch (Exception ex)
            {
                return 1;
            }
            return 0;

        }



        private void regist_Click(object sender, EventArgs e)
        {
            try
            {
                if (validaDados() == 1) return;
                FormatoAvaliacaoCliente form_av = regfCli.registar(0);
                registarQuestoes(form_av);
                FormatoClienteCont aux;
                aux = anterior;
                while (aux != null)
                {
                    aux.registarQuestoes(form_av);
                    aux = aux.anterior;
                }

                if (gestor_analise.registaFormatoAvaliacao(form_av, 1, gestor_analise.AmbComercialSessao.Cabeçalho.Cod) == 0)
                    MessageBox.Show("Formato registado com Sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Visible = false;
                menuAnaliseetda.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ocorreu um erro no registo!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void registarQuestoes(FormatoAvaliacaoCliente form_av)
        {

            try
            {

                int linhas = consideracao1.Rows.Count, i = 0;
                //List<QuestaoCliente> questoes = new List<QuestaoCliente>();
                while (linhas > i + 1)
                {
                    form_av.registMin(consideracao1.Rows[i].Cells[0].Value.ToString(), Convert.ToInt32(consideracao1.Rows[i].Cells[1].Value), Convert.ToInt32(consideracao1.Rows[i].Cells[2].Value), Convert.ToInt32(consideracao1.Rows[i].Cells[3].Value), Convert.ToInt32(consideracao1.Rows[i].Cells[4].Value) / 100f, lConsideracoes[indiceConsi].Cod);

                    i++;
                }

                if (consideracao2.Visible || this.Visible == false)
                {
                    i = 0;
                    linhas = consideracao2.Rows.Count;
                    while (linhas > i + 1)
                    {
                        form_av.registMin(consideracao2.Rows[i].Cells[0].Value.ToString(), Convert.ToInt32(consideracao2.Rows[i].Cells[1].Value), Convert.ToInt32(consideracao2.Rows[i].Cells[2].Value), Convert.ToInt32(consideracao2.Rows[i].Cells[3].Value), Convert.ToInt32(consideracao2.Rows[i].Cells[4].Value) / 100f, lConsideracoes[indiceConsi + 1].Cod);
                        i++;
                    }
                    if (consideracao3.Visible || this.Visible == false)
                    {
                        i = 0;
                        linhas = consideracao3.Rows.Count;
                        while (linhas > i + 1)
                        {
                            form_av.registMin(consideracao3.Rows[i].Cells[0].Value.ToString(), Convert.ToInt32(consideracao3.Rows[i].Cells[1].Value), Convert.ToInt32(consideracao3.Rows[i].Cells[2].Value), Convert.ToInt32(consideracao3.Rows[i].Cells[3].Value), Convert.ToInt32(consideracao3.Rows[i].Cells[4].Value) / 100f, lConsideracoes[indiceConsi + 2].Cod);
                            i++;
                        }
                        if (consideracao4.Visible || this.Visible == false)
                        {
                            i = 0;
                            linhas = consideracao4.Rows.Count;
                            while (linhas > i + 1)
                            {
                                form_av.registMin(consideracao4.Rows[i].Cells[0].Value.ToString(), Convert.ToInt32(consideracao4.Rows[i].Cells[1].Value), Convert.ToInt32(consideracao4.Rows[i].Cells[2].Value), Convert.ToInt32(consideracao4.Rows[i].Cells[3].Value), Convert.ToInt32(consideracao4.Rows[i].Cells[4].Value) / 100f, lConsideracoes[indiceConsi + 3].Cod);
                                i++;
                            }
                        }
                    }
                }


            }
            catch (Exception exp)
            {
                MessageBox.Show("Erro ao registar formato", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    }



}

